<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <style>
        html, body {
            height: 100%;

            background-image: url(./img/bk_img_login.jpg);
            background-repeat: no-repeat;
            background-position: center;
            background-size: cover;
        }

    @keyframes fadeOut {
            0% { opacity: 1; }
            100% { opacity: 0; }
        }
        .dynamic-font {
            writing-mode: vertical-rl;
            font-size: 40px;
            animation: fadeOut 10s ease-in-out;
        }

        .hide_1 {
            opacity: 0;
            position: absolute;
            top:100px;
            left: 220px;
            animation-delay: 5s;
            color:aquamarine;
        }
        .hide_2 {
            opacity: 0;
            position: absolute;
            top:180px;
            left: 290px;
            animation-delay: 7s;
            color:aquamarine;
        }
    </style>

    <link rel="stylesheet" href="./css/common.css">
    <link rel="stylesheet" href="./css/login.css">
    <link rel="shortcut icon" href="./img/favicon.ico" >
</head>
<body>
    <div class="nav">
        休闲游戏平台
    </div>
    <h1 class="dynamic-font hide_1">欢迎您回来</h1>
    <h1 class="dynamic-font hide_2">祝您游戏愉快</h1>
    <div class="login-container">
        <!-- 登录界面的对话框 -->
        <div class="login-dialog">
            <!-- 提示信息 -->
            <h3>登录</h3>
            <!-- 这个表示一行 -->
            <div class="row">
                <img src="./img/user.png" alt="加载失败" height="40" width="40"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <input type="text" id="user_name">
            </div>
            <!-- 这是另一行 -->
            <div class="row">
                <img src="./img/password.png" alt="加载失败" height="35" width="35"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <input type="password" id="password">
            </div>
            <!-- 提交按钮 -->
            <div class="row">
                <button id="regist" onclick="regist()">注册</button>
                <button id="submit" onclick="login()">登录</button>
            </div>
        </div>

    </div>

    <script src="js/jquery.min.js"></script>
    <script>
        //1. 给按钮添加点击事件，调用登录请求函数
        //2. 封装登录请求函数
        function login() {
            //  1. 获取输入框中的用户名和密码，并组织json对象
            var user_msg = {
                username: document.getElementById("user_name").value,
                password: document.getElementById("password").value
            }
            //  2. 通过ajax向后台发送登录验证请求
            $.ajax({
                url:"/login",
                type:"POST",
                data: JSON.stringify(user_msg),
                success:function(res){
                    if (res.result == false)
                    {
                        alert(res.reason);
                        document.getElementById("user_name").value = "";
                        document.getElementById("password").value = "";
                    }
                    else{
                        location.replace("./game_hall.html");
                    }
                },
                error:function(xhr){
                    alert(JSON.stringify(xhr)); 
                    document.getElementById("user_name").value = "";
                    document.getElementById("password").value = "";
                }
            })
            
        }

        function regist() {
            window.location.assign("/register.html");
        }

    </script>
</body>
</html>